package com.jilujia.web.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jilujia.domain.blog.Blog;
import com.jilujia.domain.blog.service.BlogService;
import com.jilujia.domain.hotel.Hotel;
import com.jilujia.domain.hotel.service.HotelService;
import com.jilujia.domain.question.Question;
import com.jilujia.domain.question.service.QuestionService;
import com.jilujia.domain.region.Region;
import com.jilujia.domain.region.service.RegionService;
import com.jilujia.framework.exception.ResourceNotFoundException;

@Controller
public class RegionsController extends AbstractWebController {

	private static final Logger LOG = LoggerFactory.getLogger(RegionsController.class);

	@Autowired
	private HotelService hotelService;

	@Autowired
	private RegionService regionService;

	@Autowired
	private QuestionService questionService;

	@Autowired
	private BlogService blogService;

	public RegionsController() {
	}

	@RequestMapping(value = "/regions/{id}", method = RequestMethod.GET)
	public String show(@PathVariable Long id, Model model) {
		Region region = regionService.findRegionById(id);

		if (region == null) {
			throw new ResourceNotFoundException(id);
		}

		List<Hotel> hotels = hotelService.findTop10HotelsByRegion(region);

		List<Question> questions = questionService.findTop10QuestionsByRegion(region);

		List<Blog> blogs = blogService.findTop10BlogsByRegion(region);

		model.addAttribute(region);
		model.addAttribute(hotels);
		model.addAttribute(questions);
		model.addAttribute(blogs);

		return "regions/show";
	}

	@RequestMapping(value = "/regions", method = RequestMethod.GET)
	public String list(Model model) {
		List<Region> regions = regionService.findAllRegions();
		model.addAttribute(regions);
		return "regions/list";
	}

}
